package com.library.management.controller.book;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.management.common.Result;
import com.library.management.domain.book.Book;
import com.library.management.service.book.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class BookController {
    
    @Autowired
    private BookService bookService;
    
    /**
     * 分页查询图书列表
     */
    @GetMapping("/list")
    public Result<Page<Book>> getBookList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer status) {
        try {
            Page<Book> pageParam = new Page<>(page, size);
            LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
            
            if (keyword != null && !keyword.trim().isEmpty()) {
                wrapper.like(Book::getTitle, keyword)
                      .or()
                      .like(Book::getAuthor, keyword)
                      .or()
                      .like(Book::getIsbn, keyword)
                      .or()
                      .like(Book::getPublisher, keyword);
            }
            
            if (category != null && !category.trim().isEmpty()) {
                wrapper.eq(Book::getCategory, category);
            }
            
            if (status != null) {
                wrapper.eq(Book::getStatus, status);
            }
            
            wrapper.orderByDesc(Book::getCreateTime);
            Page<Book> result = bookService.page(pageParam, wrapper);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询图书列表失败: " + e.getMessage());
        }
    }

    
    /**
     * 添加图书
     */
    @PostMapping("/add")
    public Result<String> addBook(@RequestBody Book book) {
        try {
            boolean success = bookService.addBook(book);
            if (success) {
                return Result.success("添加图书成功");
            } else {
                return Result.error("添加图书失败");
            }
        } catch (Exception e) {
            return Result.error("添加图书失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新图书
     */
    @PutMapping("/update")
    public Result<String> updateBook(@RequestBody Book book) {
        try {
            boolean success = bookService.updateBook(book);
            if (success) {
                return Result.success("更新图书成功");
            } else {
                return Result.error("更新图书失败");
            }
        } catch (Exception e) {
            return Result.error("更新图书失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除图书
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteBook(@PathVariable Long id) {
        try {
            boolean success = bookService.deleteBook(id);
            if (success) {
                return Result.success("删除图书成功");
            } else {
                return Result.error("删除图书失败");
            }
        } catch (Exception e) {
            return Result.error("删除图书失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新图书状态
     */
    @PutMapping("/{id}/status")
    public Result<String> updateBookStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            boolean success = bookService.updateBookStatus(id, status);
            if (success) {
                return Result.success("更新图书状态成功");
            } else {
                return Result.error("更新图书状态失败");
            }
        } catch (Exception e) {
            return Result.error("更新图书状态失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量添加图书
     */
    @PostMapping("/batchAdd")
    public Result<String> batchAddBooks(@RequestBody List<Book> books) {
        try {
            boolean success = bookService.batchAddBooks(books);
            if (success) {
                return Result.success("批量添加图书成功");
            } else {
                return Result.error("批量添加图书失败");
            }
        } catch (Exception e) {
            return Result.error("批量添加图书失败: " + e.getMessage());
        }
    }
} 